package com.jp.knowledge.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.jp.knowledge.R;
import com.jp.knowledge.adapter.skill.PositionKnolegeContentAdapter;
import com.jp.knowledge.model.PositionKnowlegeMode;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;

import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/7/12.
 */

public class JobKnowledgeDataAdapter extends RecycleVAdapter<PositionKnowlegeMode.ListBeanX> implements PositionKnolegeContentAdapter.ItemClickListener {

	public JobKnowledgeDataAdapter(Context context, @NonNull List<PositionKnowlegeMode.ListBeanX> list) {
		super(context, list);
	}

	@Override
	public int getLayoutId(int viewType) {
		return R.layout.skill_content_view;
	}

	@Override
	public void onBind(RecycleVHolder holder, int position) {
		PositionKnowlegeMode.ListBeanX data = getData(position);
		holder.setTextView(R.id.skill_content_title, data.getName());
		RecyclerView recyclerView = holder.get(R.id.skill_content_view);
		recyclerView.setHasFixedSize(true);
		if(recyclerView.getLayoutManager() == null || !(recyclerView.getLayoutManager() instanceof GridLayoutManager))
			recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
		RecyclerView.Adapter adapter = recyclerView.getAdapter();
		if(adapter == null || !(adapter instanceof PositionKnolegeContentAdapter)){
			adapter = new PositionKnolegeContentAdapter(mContext, data.getList());
			recyclerView.setAdapter(adapter);
		} else{
			((PositionKnolegeContentAdapter)adapter).updateAdapter(data.getList());
		}
		recyclerView.setTag(position);
		((PositionKnolegeContentAdapter)adapter).setMainPositon(position);
		((PositionKnolegeContentAdapter)adapter).setItemClickListener(this);
		holder.get(R.id.line).setVisibility(position == 0 ? View.GONE : View.VISIBLE);
	}

	@Override
	public void onItemClick(int mainPosition, int subPosition) {
		if(itemClickListener != null)
			itemClickListener.onItemClick(mainPosition, subPosition);
	}

	private ItemClickListener itemClickListener;
	public void setItemClickListener(ItemClickListener itemClickListener){
		this.itemClickListener = itemClickListener;
	}

	public interface ItemClickListener{
		void onItemClick(int mainPosition, int subPosition);
	}
}
